//
//  MPNetworkService.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/5.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPNetworkService.h"
#import "MPDeviceObject.h"
#import "MPApiUrlConstant.h"
#import "JSONKit.h"

@implementation MPNetworkService

#pragma mark - general

/*
 deviceID，设备ID（必须）
 regUserID，百度绑定返回的userID（必须）
 regChannelID，百度绑定返回的channelID（必须）
 appId 百度绑定返回的
 model ,手机型号（可选）
 osversion ，系统，系统版本（可选）
 deviceType ,  1->iPhone ,2->Android , 3->其他（可选）
 */
//登记设备
+ (void)registerDeviceWithObject:(MPDeviceObject *)device onComletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary* paramDic = [NSMutableDictionary dictionary];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSNumber numberWithInteger:device.deviceType] forKey:@"deviceType"];

    if (![NSString stringIsEmpty:device.deviceId]) {
        [dic setObject:device.deviceId forKey:@"deviceID"];
    }
    
    if (![NSString stringIsEmpty:device.bRegisterUserId]) {
        [dic setObject:device.bRegisterUserId forKey:@"regUserID"];
    }
    
    if (![NSString stringIsEmpty:device.bRegisterChannelId]) {
        [dic setObject:device.bRegisterChannelId forKey:@"regChannelID"];
    }
    
    if (![NSString stringIsEmpty:device.bRegisterAppId]) {
        [dic setObject:device.bRegisterAppId forKey:@"appId"];
    }

    if (![NSString stringIsEmpty:device.deviceModel]) {
        [dic setObject:device.deviceModel forKey:@"model"];
    }

    if (![NSString stringIsEmpty:device.osVersionString]) {
        [dic setObject:device.osVersionString forKey:@"osversion"];
    }
    [paramDic setObject:[dic JSONString] forKey:@"deviceInfo"];

    [request sendPostRequestWithMethod:kMPUrlRegisterDevice param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

#pragma mark - bid相关
/*
 参数： deviceID：string，设备ID（deviceID=XXXX）POST请求
 visitorSubscribeType:未登录用户首次登陆APP时，可以选择三类“新手上路”（visitorSubscribeType=1）、“稳健投资（最新已改成收益为王）”（visitorSubscribeType=2）、“收益为主（最新已改名为快进快出）” （visitorSubscribeType=3）；
 该参数在选择“跳过”时（visitorSubscribeType=0）；
 indexParam: {"last":"rate","platform":"asc","rate":"asc","month":"desc","progress":"desc"}  last表示按哪种排序规则排序，值可以是{default默认排序、platform按平台排序、 rate按年化收益排序 、month按投资期限排序 、progress按进度排序} ， rate、 platform、month、 progress的值为 desc asc两者之一 ；由于只是进行单个条件排序，因此其他条件为空，如按受益排序，传值为 {"last":"rate",“platform”：”“，"rate":"asc","month":"","progress":""} ；默认时 {"last":"default","platform":"","rate":"","month":"","progress":""}
 */

//热标接口
+ (void)fetchHotBidWithDeviceId:(NSString *)dId subscribeType:(eMPVisitorSubscribeType)subsType sortType:(eMPSortType)sortType upOrDown:(eMPSort)upOrDown onComletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    [paramDic setObject:dId forKey:@"deviceID"];
    [paramDic setObject:[NSNumber numberWithInteger:subsType] forKey:@"visitorSubscribeType"];
    
    NSMutableDictionary *indexParamDic = [[NSMutableDictionary alloc] init];
    switch (sortType) {
        case eMPSortTypeDefault:
        {
            [indexParamDic setObject:@"default" forKey:@"last"];
            [indexParamDic setObject:@"" forKey:@"platform"];
            [indexParamDic setObject:@"" forKey:@"rate"];
            [indexParamDic setObject:@"" forKey:@"month"];
            [indexParamDic setObject:@"" forKey:@"progress"];
        }
            break;
        case eMPSortTypePlatform:
        {
            [indexParamDic setObject:@"platform" forKey:@"last"];
            [indexParamDic setObject:@"" forKey:@"rate"];
            [indexParamDic setObject:@"" forKey:@"month"];
            [indexParamDic setObject:@"" forKey:@"progress"];
            if (upOrDown == eMPSortUp) {
                [indexParamDic setObject:@"asc" forKey:@"platform"];
            }
            else if (upOrDown == eMPSortDown)
            {
                [indexParamDic setObject:@"desc" forKey:@"platform"];
            }
        }
            break;
        case eMPSortTypeRate:
        {
            [indexParamDic setObject:@"rate" forKey:@"last"];
            [indexParamDic setObject:@"" forKey:@"rate"];
            [indexParamDic setObject:@"" forKey:@"month"];
            [indexParamDic setObject:@"" forKey:@"progress"];
            if (upOrDown == eMPSortUp) {
                [indexParamDic setObject:@"asc" forKey:@"rate"];
            }
            else if (upOrDown == eMPSortDown)
            {
                [indexParamDic setObject:@"desc" forKey:@"rate"];
            }
        }
            break;
        case eMPSortTypeMonth:
        {
            [indexParamDic setObject:@"month" forKey:@"last"];
            [indexParamDic setObject:@"" forKey:@"rate"];
            [indexParamDic setObject:@"" forKey:@"month"];
            [indexParamDic setObject:@"" forKey:@"progress"];
            if (upOrDown == eMPSortUp) {
                [indexParamDic setObject:@"asc" forKey:@"month"];
            }
            else if (upOrDown == eMPSortDown)
            {
                [indexParamDic setObject:@"desc" forKey:@"month"];
            }
        }
            break;
        case eMPSortTypeProgress:
        {
            [indexParamDic setObject:@"progress" forKey:@"last"];
            [indexParamDic setObject:@"" forKey:@"rate"];
            [indexParamDic setObject:@"" forKey:@"month"];
            [indexParamDic setObject:@"" forKey:@"progress"];
            if (upOrDown == eMPSortUp) {
                [indexParamDic setObject:@"asc" forKey:@"progress"];
            }
            else if (upOrDown == eMPSortDown)
            {
                [indexParamDic setObject:@"desc" forKey:@"progress"];
            }
        }
            break;
    }
    
    [paramDic setObject:[indexParamDic JSONString] forKey:@"indexParam"];
    
    [request sendPostRequestWithMethod:kMPUrlFetchHotBid param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

#pragma mark - news相关
//资讯接口
+ (void)fetchNewsWithInfoType:(eMPInfoType)infoType page:(NSUInteger)pageNum onComletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    switch (infoType) {
        case eMPInfoTypeP2P:
        {
            [paramDic setObject:@"p2p" forKey:@"type"];
        }
            break;
        case eMPInfoTypeZC:
        {
            
        }
            break;
    }
    
    [paramDic setObject:[NSNumber numberWithInteger:pageNum] forKey:@"page"];
    [request sendPostRequestWithMethod:kMPUrlFetchHotNews param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

#pragma mark - advertise
//广告接口
+ (void)fetchAdvertiseWithInfoType:(eMPInfoType)infoType onComletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    switch (infoType) {
        case eMPInfoTypeP2P:
        {
            [paramDic setObject:@"p2p" forKey:@"type"];
        }
            break;
        case eMPInfoTypeZC:
        {
            
        }
            break;
    }
    
    [request sendPostRequestWithMethod:kMPUrlFetchAdvertise param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

#pragma mark - subscribe
//修改我的订阅选项
+ (void)modifyMySubcribeWithDeviceId:(NSString *)dId platformCode:(NSString *)pCode rateCode:(NSString *)rCode durationCode:(NSString *)dCode onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    
    NSMutableDictionary *subscribeInfoDic = [NSMutableDictionary new];
    [subscribeInfoDic setObject:pCode forKey:@"platform"];
    [subscribeInfoDic setObject:rCode forKey:@"yearRate"];
    [subscribeInfoDic setObject:dCode forKey:@"repayLimitTime"];
    [paramDic setObject:[subscribeInfoDic JSONString] forKey:@"subscribeInfo"];
    
    [request sendPostRequestWithMethod:kMPUrlModifyMySubscribe param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

+ (void)modifyMySubcribeWithDeviceObject:(MPDeviceObject *)device onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:device.deviceId forKey:@"deviceID"];
    
    NSMutableDictionary *subscribeInfoDic = [NSMutableDictionary new];
    [subscribeInfoDic setObject:[device.platformCodeArray componentsJoinedByString:@","] forKey:@"platform"];
    [subscribeInfoDic setObject:[device.yearRateCodeArray componentsJoinedByString:@","] forKey:@"yearRate"];
    [subscribeInfoDic setObject:[device.durationCodeArray componentsJoinedByString:@","] forKey:@"repayLimitTime"];
    [paramDic setObject:[subscribeInfoDic JSONString] forKey:@"subscribeInfo"];
    
    [request sendPostRequestWithMethod:kMPUrlModifyMySubscribe param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//APP用户手动更新是否开启推送功能
+ (void)modifyPushStateWithDeviceId:(NSString *)dId state:(BOOL)isPushOn onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    if (isPushOn) {
        [paramDic setObject:[NSNumber numberWithInteger:1] forKey:@"isSwitch"];
    }
    else
    {
        [paramDic setObject:[NSNumber numberWithInteger:0] forKey:@"isSwitch"];
    }
    
    [request sendPostRequestWithMethod:kMPUrlModifyPushState param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//APP判断网络连接成功的接口
+ (void)isNetworkConnectedWithCompletionHandler:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    [request sendGetRequestWithMethod:kMPUrlNetworkState param:nil onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//所有平台列表接口
+ (void)fetchAllPlatformWithDeviceId:(NSString *)dId onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    
    [request sendPostRequestWithMethod:kMPUrlFetchPlatformList param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//获取我的订阅接口
+ (void)fetchMySubcribeInfoWithDeviceId:(NSString *)dId onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    
    [request sendPostRequestWithMethod:kMPUrlFetchMySubcribeInfo param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//添加平台
+ (void)addPlatformWithDeviceId:(NSString *)dId platformCode:(NSString *)codeString onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    [paramDic setObject:codeString forKey:@"platform"];
    
    [request sendPostRequestWithMethod:kMPUrlAddPlatform param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

//删除平台
+ (void)removePlatformWithDeviceId:(NSString *)dId platformCode:(NSString *)codeString onCompletion:(MPRequestComplete)block
{
    MPHttpRequest *request = [[MPHttpRequest alloc] init];
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    [paramDic setObject:dId forKey:@"deviceID"];
    [paramDic setObject:codeString forKey:@"platform"];
    
    [request sendPostRequestWithMethod:kMPUrlRemovePlatform param:paramDic onComplete:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        if (block) {
            block (errorNum, info, errorMsg);
        }
    }];
}

@end
